package com.symaster.common.geom.entity;

import com.symaster.common.geom.util.PointUtil;

/**
 * @author yinmiao
 * @date 2022-03-29 09:23
 */
public class Transform {

    private Double scale;
    private Double angle;
    private Vector vector;

    public Transform() {
    }

    public Transform(Double scale, Double angle, Vector vector) {
        this.scale = scale;
        this.angle = angle;
        this.vector = vector;
    }

    public static Transform generateTransform(Polygon srcPolygon, Polygon dstPolygon, Double angle) {
        Polygon clone = dstPolygon.clone();
        if (angle != null) {
            clone.rotateTransform(clone.getCenter(), -angle);
        }
        double scale = clone.getWidth() / srcPolygon.getWidth();
        Vector translateVector = PointUtil.getTranslateVector(srcPolygon.getCenter(), dstPolygon.getCenter());
        return new Transform(scale, angle, translateVector);
    }

    public Double getScale() {
        return scale;
    }

    public void setScale(Double scale) {
        this.scale = scale;
    }

    public Double getAngle() {
        return angle;
    }

    public void setAngle(Double angle) {
        this.angle = angle;
    }

    public Vector getVector() {
        return vector;
    }

    public void setVector(Vector vector) {
        this.vector = vector;
    }

    public Polygon transform(Polygon polygon) {
        Polygon clone = polygon.clone();
        if (scale != null && scale != 1.0) {
            clone.scaleTransform(polygon.getCenter(), scale);
        }
        if (angle != null && angle != 0.0) {
            clone.rotateTransform(polygon.getCenter(), angle);
        }
        if (vector != null && vector.getX() != 0.0 && vector.getY() != 0.0) {
            clone.translate(vector);
        }
        return clone;
    }

    @Override
    public String toString() {
        return "Transform{" +
                "scale=" + scale +
                ", angle=" + angle +
                ", vector=" + vector +
                '}';
    }
}
